package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

public class StringConverter implements AutoConverter {

    @Override
    public Class<?>[] convertType() {
        return new Class[]{String.class};
    }

    @Override
    public String dbType(long len, int pointLen) {
        //根据配置长度自动升级类型varchar -> MEDIUMTEXT -> LONGTEXT
        DbColumnType dbColumnType = DbColumnType.VARCHAR;
        if(len > dbColumnType.getMaxLen()){
            dbColumnType = DbColumnType.MEDIUMTEXT;
        }
        if(len > dbColumnType.getMaxLen()){
            dbColumnType = DbColumnType.LONGTEXT;
        }
        return dbColumnType.format(len,pointLen);
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.VARCHAR;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        return value;
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
        return value;
    }
}
